<?php

namespace app\api\model\wanlshop;

use addons\ymypaylinx\library\Paylinx;
use think\Cache;
use think\Model;
use traits\model\SoftDelete;

class Pay extends Model
{

    use SoftDelete;

    

    // 表名
    protected $name = 'wanlshop_pay';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = 'int';

    // 定义时间戳字段名
    protected $createTime = 'createtime';
    protected $updateTime = 'updatetime';
    protected $deleteTime = 'deletetime';
    protected $append = [
        'rmb_price',
    ];
	
	//写入 order_pay_no
	public function getPayNo($value)
	{
		// 高并发有几率重复，暂时不需要
	    return date('YmdHis').substr(strrev($value),-8).substr(implode(NULL, array_map('ord', str_split(substr(uniqid(), 7, 13), 1))), 0, 8);
	}


	public function getRmbPrice($value,$data){
	    if(isset($data['price']) && $data['price'] > 0){
            $currencyRate = $this->getCurrencyRate('',0);
            return bcmul($data['price'],$currencyRate,2);
        }
	    return 0;
    }

	//获取汇率
    public function getCurrencyRate($type,$shop_id){
	    $config = model('app\api\model\wanlshop\ShopConfig')->getPayConfig($type,'wap',$shop_id);
        if($type == 'paylinx_alipay'){
            $currency_rate = Cache::get('alipay_currency_rate');
            if(!$currency_rate){
                $paylinx = Paylinx::alipay($config);
                $params = [
                    'fee_type' => 'AUD',

                ];
                $currency_rate = $paylinx->exchangeRate($params);
                if($currency_rate){
                    Cache::set('alipay_currency_rate',$currency_rate,3600);
                }
            }
            return $currency_rate;
        }else{
            $currency_rate = Cache::get('wechat_currency_rate');
            if(!$currency_rate){
                $paylinx = Paylinx::wechat($config);
                $params = [
                    'fee_type' => 'AUD',
                ];
                $currency_rate = $paylinx->exchangeRate($params);
                if($currency_rate){
                    Cache::set('wechat_currency_rate',$currency_rate,3600);
                }
            }
            return $currency_rate;
        }
    }

    //创建Paylinx支付统一订单
    public function createPaylinx($type,$method,$shop_id,$params){
        $config = model('app\api\model\wanlshop\ShopConfig')->getPayConfig($type,$method,$shop_id);
        if($type == 'paylinx_wechat'){
            $app = Paylinx::wechat($config);
            switch ($method){
                case 'app':
                    $result = $app->app($params);
                    break;
                case 'wap':

                    $result = $app->h5($params);
                    break;
                case 'scan':
                    $payResult = $app->scan($params);
                    $result = addon_url('qrcode/index/build',['text'=>$payResult['code_url']],true,true);
            }
        }else{
            $app = Paylinx::alipay($config);
            switch ($method){
                case 'app':
                    $result = $app->app($params);
                    break;
                case 'wap':
                    $result = $app->h5($params);
                    break;
                case 'scan':
                    $result = $app->scan($params);
            }
        }

        return $result;
    }
}